package com.athuahua.spzx.controller.cargo;

import cn.hutool.core.date.DateTime;
import com.athuahua.spzx.annotation.AutoLog;
import com.athuahua.spzx.entity.ProductSpec;
import com.athuahua.spzx.service.ProductSpecService;
import com.athuahua.spzx.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: huahua
 * @Date: 2024/7/31 0031
 * @Time: 15:26
 * @Description:
 */
@RestController
@RequestMapping("admin/product/productSpec")
@Tag(name = "商品规格管理接口")
@Slf4j
public class ProductSpecController {

    @Resource
    private ProductSpecService productSpecService;

    //pageInfo
    @GetMapping("pageInfo/{page}/{limit}")
    @Cacheable(cacheNames = "productSpecPageInfo", key = "{#p0,#p1}")
    public Result pageInfo(@PathVariable("page") Integer page,
                           @PathVariable("limit") Integer limit) {
        IPage<ProductSpec> list = productSpecService.pageInfo(page, limit);

        Map<String,Object> map = new HashMap<>();
        map.put("list",list.getRecords());
        map.put("total",list.getTotal());
        return Result.success(map);
    }

    //save
    @PostMapping("save")
    @Caching(evict = {
            @CacheEvict(cacheNames = "productSpecPageInfo",allEntries=true),
            @CacheEvict(cacheNames = "c_category_product_index", allEntries = true),
    })
    @AutoLog(title = "新增商品规格", businessType = 1) //记录日志使用
    public Result save(@RequestBody ProductSpec productSpec) {
        log.info("save productSpec: " + productSpec);
        productSpecService.save(productSpec);
        return Result.success();
    }

    //update
    @PutMapping("update")
    @Caching(evict = {
            @CacheEvict(cacheNames = "productSpecPageInfo",allEntries=true),
            @CacheEvict(cacheNames = "c_category_product_index", allEntries = true),
    })
    @AutoLog(title = "修改商品规格", businessType = 2) //记录日志使用
    public Result update(@RequestBody ProductSpec productSpec) {
        log.info("update productSpec: " + productSpec);
//        productSpec.setUpdateTime(DateTime.now());
        productSpecService.updateById(productSpec);
        return Result.success();
    }

    //delete
    @DeleteMapping("delete/{id}")
    @Caching(evict = {
            @CacheEvict(cacheNames = "productSpecPageInfo",allEntries=true),
            @CacheEvict(cacheNames = "c_category_product_index", allEntries = true),
    })
    @AutoLog(title = "删除商品规格", businessType = 3) //记录日志使用
    public Result delete(@PathVariable("id") Integer id) {
        productSpecService.removeById(id);
        return Result.success();
    }

    //查询所以商品规格
    @GetMapping("findAll")
    public Result<List<ProductSpec>> findAll() {
        return Result.success(productSpecService.list());
    }
}
